<?php

namespace App\Validation;

use Phalcon\Validation\Validator\PresenceOf;
use Phalcon\Validation\Validator\Numericality;
use Phalcon\Validation\Validator\InclusionIn;

class CatsValidation extends BaseValidation
{
	public function initialize()
    {
		
		$this->add(
			"id",
			new Numericality(
				  [
					  "message" => ":field is not numeric",
					  "allowEmpty" => true,
				  ]
			)
		);

		$this->add(
			"name",
			new PresenceOf(
				  [
					  "message" => ":field is required",
					  // "allowEmpty" => true,
				  ]
			)
		);

		$this->add(
			"group_id",
			new Numericality(
				  [
					  "message" => ":field is not numeric",
				  ]
			)
		);

		$this->add(
			"group_id",
			new InclusionIn(
				  [
					  "field" => "group_id",
					  "domain" => [1,2,3,4,6],//分组1、房源;2、资讯；3、装修；4、数据；6、视频
					  "message" => ":field is not included in 1 or 2 or 3 or 4 or 6",
				  ]
			)
		);
			
    }

	//验证之前
	public function beforeValidation($data, $entity, $messages)
    {
		$data->id = property_exists($data,'id') ? $data->id : NULL;
		$data->name = property_exists($data,'name') ? $data->name : NULL;
		$data->group_id = property_exists($data,'group_id') ? $data->group_id : NULL;

        return true;
    }
}